/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.user.domain.SysXbAppUser;
import com.xunyin.modules.user.service.SysXbAppUserService;
import com.xunyin.modules.user.service.dto.SysXbAppUserDto;
import com.xunyin.modules.user.service.dto.SysXbAppUserQueryCriteria;
import com.xunyin.modules.user.service.mapper.SysXbAppUserMapper;
import com.xunyin.utils.CommonConst;
import com.xunyin.utils.FileUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author admin
 * @date 2022-10-18
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "sysXbAppUser")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysXbAppUserServiceImpl extends BaseServiceImpl<SysXbAppUserMapper, SysXbAppUser> implements SysXbAppUserService {

    private final IGenerator generator;

    @Override
    public List<SysXbAppUser> selectSysXbAppUserList(String phoneNumber, String openid) {
        LambdaQueryWrapper<SysXbAppUser> wrapper = new LambdaQueryWrapper<>();
        if (phoneNumber != null && !phoneNumber.equals("")) {
            wrapper.eq(SysXbAppUser::getTelephone, phoneNumber);
        }
        wrapper.eq(SysXbAppUser::getAppId, openid);
        wrapper.eq(SysXbAppUser::getStatus, CommonConst.ENABLE_FLAG);
        List<SysXbAppUser> list = baseMapper.selectList(wrapper);
        return list;

    }


    @Override
    //@Cacheable
    public Map<String, Object> queryAll(SysXbAppUserQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<SysXbAppUser> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<SysXbAppUser> queryAll(SysXbAppUserQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(SysXbAppUser.class, criteria));
    }


    @Override
    public void download(List<SysXbAppUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysXbAppUserDto sysXbAppUser : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("微信ID", sysXbAppUser.getAppId());
            map.put("微信APPID", sysXbAppUser.getWxAppId());
            map.put("开放平台ID", sysXbAppUser.getUnionId());
            map.put("手机号", sysXbAppUser.getTelephone());
            map.put(" appType", sysXbAppUser.getAppType());
            map.put("创建日期", sysXbAppUser.getCreateTime());
            map.put("更新日期", sysXbAppUser.getUpdateTime());
            map.put("删除日期", sysXbAppUser.getDeleteTime());
            map.put("创建用户ID", sysXbAppUser.getCreateUserId());
            map.put("更新用户ID", sysXbAppUser.getUpdateUserId());
            map.put("状态", sysXbAppUser.getStatus());
            map.put(" userStatus", sysXbAppUser.getUserStatus());
            map.put(" isDel", sysXbAppUser.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
